#!/usr/bin/env python
# encoding: utf-8

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import numpy as np

class drawWidget(QWidget):
    def __init__(self, parent=None):
        super(drawWidget, self).__init__(parent)
        self.setAutoFillBackground(True)
        #  palette = self.palette()
        #  palette.setColor(QPalette.Background, QColor(255, 255, 255))
        #  self.setPalette(palette)
        #  self.setWindowFlags(Qt.FramelessWindowHint)# 无窗体
        #  self.setAttribute(Qt.WA_TranslucentBackground)# 背景透明
        self.TextFont      = QFont("Arial", 10, QFont.Bold)
        self.stepTextFont      = QFont("Arial", 10, QFont.Bold)
        self.pen = [
            QPen(QColor(145,44,238)),
            QPen(QColor(132,112,255)),
            QPen(QColor(32,205,187)),
            QPen(QColor(255,0,255)),
            QPen(QColor(255,127,0)),
            QPen(QColor(255,215,0)),
        ]
        self.color = [
            QColor(145,44,238),
            QColor(132,112,255),
            QColor(32,205,187),
            QColor(255,0,255),
            QColor(255,127,0),
            QColor(255,215,0),
        ]
        self.step =[3,4,3]
        self.pos = [0,40,80,100]
        self.road = []
        self.direction= ''
        self.raySize = 10
        self.SSD = 100
        self.direction = 'x'
        # 扫描变量
        self.scanStep = 1
        self.scan_point_index = 0
        self.scanDirection = 'x'
        self.scanPoints = []

        self.pdd_oar_ray = 'pdd'
        if self.pdd_oar_ray == 'pdd':
            self.setFixedSize(600, 300)
        if self.pdd_oar_ray == 'oar':
            self.setFixedSize(600, 300)
        if self.pdd_oar_ray == 'ray':
            self.setFixedSize(600, 600)
        if self.pdd_oar_ray == 'scan':
            self.setFixedSize(600, 600)
        self.timer = QTimer()
        self.timer.timeout.connect(self.TimerMove)
        self.timer.start(50)
        self.updateScanRange(self.raySize,self.scanStep,'A->B')

    def paintEvent(self,e):
        painter = QPainter()
        painter.begin(self)
        painter.setRenderHints(QPainter.Antialiasing|QPainter.HighQualityAntialiasing)#设置反锯齿
        if self.pdd_oar_ray == 'oar':
            self.drawOarStep(painter)
        if self.pdd_oar_ray == 'pdd':
            self.drawPddStep(painter)
        if self.pdd_oar_ray == 'ray':
            self.drawRay(painter)
        if self.pdd_oar_ray == 'direction':
            self.drawDirection(painter)
        if self.pdd_oar_ray == 'scan':
            self.drawScan(painter)
        painter.end()

    def drawOarStep(self,painter):
        h = self.height()/2
        w = self.width()/2
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        # 系数
        d_x_1 = (9 / 10) * w
        d_y_1 = (7 / 10) * h
        step = self.step
        pos = self.pos
        road = self.road
        # xy方向可能不同,需要转换road路线x和y
        if road[0][0] == 0 and road[1][0] == 0:
            road = [[r[1],r[0],r[2]] for r in road]
        if not len(pos)>1:
            return
        max_x = max(pos)
        # 移动坐标到左侧
        painter.translate(w,h)
        # 坐标转换比例系数
        perc = d_x_1/max(pos)
        painter.setFont(QFont("Arial", 6, QFont.Bold))
        # 画坐标轴
        painter.setPen(QPen(QColor(100, 0, 0), 2));
        painter.drawLine(-max_x*perc, d_y_1 , max_x*perc,d_y_1 ) #上
        for p in pos:
            painter.setPen(QPen(QColor(250, 0, 0), 2));
            # 正向
            painter.drawLine(p*perc, d_y_1 , p*perc, d_y_1 -10)
            painter.drawText(p*perc, d_y_1 , d_x_1/3, d_y_1/2, Qt.AlignLeft, '%s'%p)
            # 负向
            p = -p
            painter.drawLine(p*perc, d_y_1 , p*perc, d_y_1 -10)
            painter.drawText(p*perc, d_y_1 , d_x_1/3, d_y_1/2, Qt.AlignLeft, '%s'%p)
        # 区分每个路段
        new_road = []
        for j in range(1,len(pos)):
            temp_road= []
            for i in range(0, len(road)):
                # oar路径取第一个值
                p = road[i][0]
                if pos[j-1]<=p<=pos[j]:
                    temp_road.append(p)
            new_road.append(temp_road)
        # 每个路段用不同高度画线
        grade = 20
        for i in range(0, len(new_road)):
            painter.setPen(self.pen[i])
            h = d_y_1*grade/30
            grade = grade - 2
            r = new_road[i]
            if r:
                # 正向
                painter.drawText((min(r))*perc, -(h+20), d_y_1/2, d_y_1/2, Qt.AlignLeft, '%.1f'%step[i])
                painter.drawLine(r[0]*perc, -h, r[-1]*perc, -h)
                for p in r:
                    painter.drawLine(p*perc, d_y_1, p*perc, -h)
                # 负向
                r = [ -i for i in r]
                painter.drawText((min(r))*perc, -(h+20), d_y_1/2, d_y_1/2, Qt.AlignLeft, '%.1f'%step[i])
                painter.drawLine(r[0]*perc, -h, r[-1]*perc, -h)
                for p in r:
                    painter.drawLine(p*perc, d_y_1, p*perc, -h)
        return

    def drawPddStep(self,painter):
        h = self.height()/2
        w = self.width()/2
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        # 系数
        d_x_1 = (9 / 10) * w
        d_y_1 = (7 / 10) * h

        step = self.step
        pos = self.pos
        road = self.road
        if not len(pos)>1:
            return
        max_x = max(pos)
        # 移动坐标到左侧
        painter.translate(w-d_x_1,h)
        # 坐标转换比例系数
        perc = d_x_1/max(pos)*2
        painter.setFont(QFont("Arial", 8, QFont.Bold))
        # 画刻度线
        painter.setPen(QPen(QColor(100, 0, 0), 2));
        painter.drawLine(0, d_y_1 , max_x*perc,d_y_1 ) #上
        for p in pos:
            painter.setPen(QPen(QColor(250, 0, 0), 2));
            painter.drawLine(p*perc, d_y_1 , p*perc,d_y_1 -10)
            painter.drawText(p*perc, d_y_1 , d_x_1/3, d_y_1/2, Qt.AlignLeft, '%s'%p)
        # 区分每个路段
        new_road = []
        for j in range(1,len(pos)):
            temp_road= []
            for i in range(0, len(road)):
                # pdd路径取第二个值
                p = road[i][2]
                if pos[j-1]<=p<=pos[j]:
                    temp_road.append(p)
            new_road.append(temp_road)
        # 每个路段用不同高度画线
        grade = 20
        for i in range(0, len(new_road)):
            painter.setPen(self.pen[i])
            h = d_y_1*grade/30
            grade = grade - 2
            r = new_road[i]
            if r:
                painter.drawText((min(r))*perc, -(h+20), d_y_1/2, d_y_1/2, Qt.AlignLeft, '%.1f'%step[i])
                painter.drawLine(r[0]*perc, -h, r[-1]*perc, -h)
                for p in r:
                    painter.drawLine(p*perc, d_y_1, p*perc, -h)

    def drawPddStep_bk(self,painter):
        h = self.height()/2
        w = self.width()/2
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        # 系数
        d_x_1 = (9 / 10) * w
        d_y_1 = (7 / 10) * h

        step = self.step
        pos = self.pos
        if not len(pos)>1:
            return

        max_x = max(pos)
        # 移动坐标到左侧
        painter.translate(w-d_x_1,h)
        # 坐标转换比例系数
        perc = d_x_1/max(pos)*2
        painter.setFont(QFont("Arial", 8, QFont.Bold))
        # 放射源水平线
        # 画坐标轴
        #  painter.setFont(self.TextFont);
        painter.setPen(QPen(QColor(100, 0, 0), 2));
        painter.drawLine(0, d_y_1 , max_x*perc,d_y_1 ) #上
        for i in range(0,int(max_x)+1):
            if i%10== 0:
                # 画10位置的刻度线
                painter.setPen(QPen(QColor(250, 0, 0), 2));
                painter.drawLine(i*perc, d_y_1 , i*perc,d_y_1 -10)
                painter.drawText(i*perc, d_y_1 , d_x_1/3, d_y_1/2, Qt.AlignLeft, '%s'%i)

        # 所有坐标位置
        pos_x = []
        painter.setFont(self.stepTextFont);
        for i in range(1,len(pos)):
            w = pos[i]-pos[i-1]
            end = 0
            h = d_y_1*(5-i)/5
            if i == 1:
                painter.drawLine(0, d_y_1, 0, -h)
                painter.drawText(pos[i-1]*perc+d_y_1/2, -h*1.5, d_y_1/2, d_y_1/2, Qt.AlignLeft, '%.1f'%step[i-1])
            else:
                painter.drawText(pos[i-1]*perc+d_y_1/2, -h*1.5, d_y_1/2, d_y_1/2, Qt.AlignLeft, '%.1f'%step[i-1])
            painter.setPen(self.pen[i-1])
            painter.drawLine(pos[i-1]*perc, -h, pos[i]*perc, -h)
            for j in range(1,int(w)+1):
                if j < w:
                    if j%step[i-1]== 0:
                        p = pos[i-1]+j
                        pos_x.append(p)
                        # 正
                        painter.drawLine(p*perc, d_y_1, p*perc, -h)
                        # 负
                        #  painter.drawLine(-p*perc, axis_y, -p*perc, -h)
            p = pos[i-1]+j
            pos_x.append(p)
            painter.drawLine(p*perc, d_y_1, p*perc, -h)

    def drawRay(self,painter):
        h = self.height()/2
        w = self.width()/2
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        # 移动坐标系到中心
        painter.translate(w,h)
        # 系数
        d_x_1 = (9 / 10) * w
        d_y_1 = (9 / 10) * h
        painter.setFont(QFont("Arial", 12*d_x_1/1.8/w, QFont.Bold))
        # 画矩形 - 水箱
        painter.setBrush(QColor(125, 255, 255));
        painter.drawRect(-d_x_1, 0, 2*d_x_1, d_y_1);
        # 中心线
        painter.drawLine(0, -d_y_1, 0, d_y_1)
        d_x_2 = (5 / 10) * w * (self.raySize/60)
        d_y_2 = (5 / 10) * h
        # 放射源水平线
        # painter.drawLine(-d_x_2, 0, d_x_2,0)
        painter.drawLine(-d_x_2, -d_y_1 , d_x_2,-d_y_1 ) #上
        painter.drawText(-d_x_1/4, -d_y_1 , d_x_1/1.8,-d_y_1/10, Qt.AlignCenter, '射线照射')
        # 射野-红色
        painter.setPen(QPen(QColor(0, 0, 100), 5));
        painter.drawLine(-d_x_2, 0, d_x_2,0)
        painter.drawText(-d_x_2, d_y_2/3 , d_x_1/1.8,-d_y_1/10, Qt.AlignCenter, '射野:%.1fcm'%self.raySize)
        # 射野光线边缘线
        painter.setPen(QPen(QColor(200, 0, 0), 3));
        per = d_x_2/d_y_1
        d_x_3 = (d_y_1+d_y_1/2)*per
        d_y_3 = d_y_1/2
        painter.drawLine(0, -d_y_1, d_x_3 , d_y_3) #左斜线
        painter.drawLine(0, -d_y_1, -d_x_3 , d_y_3) #右斜线
        # SSD
        painter.setPen(QPen(QColor(0, 100, 0), 3));
        painter.drawLine(d_x_2, 0, d_x_2, -d_y_1)
        painter.drawText(d_x_2, -d_y_1/2, d_x_1/2,-d_y_1/10, Qt.AlignCenter, '源波距:%.1fcm'%self.SSD)

    def drawDirection(self,painter):
        h = self.height()/2
        w = self.width()/2
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        # 移动坐标系到中心
        painter.translate(w,h)
        # 系数
        d_x_1 = (9 / 10) * w
        d_y_1 = (9 / 10) * h
        painter.setFont(QFont("Arial", 12*d_x_1/1.8/w, QFont.Bold))
        # 画矩形 - 水箱
        painter.setBrush(QColor(125, 255, 255));
        painter.drawRect(-d_x_1, -d_y_1, 2*d_x_1, 2*d_y_1);
        # 画矩形 - 射野
        painter.setBrush(QColor(25, 5, 155));
        painter.drawRect(-d_x_1*2/3, -d_y_1*2/3, 2*d_x_1*2/3, 2*d_y_1*2/3);
        painter.drawText(-d_x_1/4, -d_y_1 , d_x_1/1.8,-d_y_1/10, Qt.AlignCenter, '方向顶视图')

        painter.setFont(QFont("Arial", 20, QFont.Bold))
        painter.drawText(d_x_1/2, 0 , d_x_1/1.8,-d_y_1/5, Qt.AlignCenter, 'B')
        painter.drawText(-d_x_1, 0 , d_x_1/1.8,-d_y_1/5, Qt.AlignCenter, 'A')
        painter.drawText(-d_x_1/3, -d_y_1*2/3 , d_x_1/1.8,-d_y_1/5, Qt.AlignCenter, 'T')
        painter.drawText(-d_x_1/3, d_y_1*2/2 , d_x_1/1.8,-d_y_1/5, Qt.AlignCenter, 'G')
        # 画xy - 射野
        #  painter.setBrush(QColor(25, 255, 255));
        painter.setPen(QPen(QColor(0, 200, 0), 3));
        # x线
        painter.drawLine(-d_x_1, 0, d_x_1, 0);
        # y线
        painter.drawLine(0, -d_y_1, 0, d_y_1);
        # x箭头
        painter.drawLine(w*9/10, 0, w*8/10, h/9);
        painter.drawLine(w*9/10, 0, w*8/10, -h/9);
        # y箭头
        painter.drawLine(0, -d_y_1, w/9, -h*8/10);
        painter.drawLine(0, -d_y_1, -w/9, -h*8/10);
        painter.setFont(QFont("Arial", 15, QFont.Bold))
        painter.drawText(d_x_1/1.5, 0,w/5,h/5, Qt.AlignCenter, 'x')
        painter.drawText(0,-d_y_1/1.1,w/5,h/5, Qt.AlignCenter, 'y')
        # 中心线
        painter.setPen(QPen(QColor(200, 0, 0), 3));
        if self.direction == 'AG->BT':
            painter.drawLine(-d_x_1*2/3, d_y_1*2/3, d_x_1*2/3, -d_y_1*2/3);
            painter.drawLine(0, 0, -d_x_1/8, 0);
            painter.drawLine(0, 0, 0, d_x_1/8);
        if self.direction == 'BT->AG':
            painter.drawLine(-d_x_1*2/3, d_y_1*2/3, d_x_1*2/3, -d_y_1*2/3);
            painter.drawLine(0, 0, d_x_1/8, 0);
            painter.drawLine(0, 0, 0, -d_x_1/8);
        if self.direction == 'BG->AT':
            painter.drawLine(-d_x_1*2/3, -d_y_1*2/3, d_x_1*2/3, d_y_1*2/3);
            painter.drawLine(0, 0, d_x_1/8, 0);
            painter.drawLine(0, 0, 0, d_x_1/8);
        if self.direction == 'AT->BG':
            painter.drawLine(-d_x_1*2/3, -d_y_1*2/3, d_x_1*2/3, d_y_1*2/3);
            painter.drawLine(0, 0, -d_x_1/8, 0);
            painter.drawLine(0, 0, 0, -d_x_1/8);
        if self.direction == 'A->B':
            painter.drawLine(-d_x_1*2/3, 0, d_x_1*2/3, 0);
            painter.drawLine(0, 0, -d_x_1/8, d_x_1/8);
            painter.drawLine(0, 0, -d_x_1/8, -d_x_1/8);
        if self.direction == 'B->A':
            painter.drawLine(d_x_1*2/3, 0, -d_x_1*2/3, 0);
            painter.drawLine(0, 0, d_x_1/8, d_x_1/8);
            painter.drawLine(0, 0, d_x_1/8, -d_x_1/8);
        if self.direction == 'G->T':
            painter.drawLine(0, d_y_1*2/3, 0 ,-d_y_1*2/3);
            painter.drawLine(0, 0, -d_y_1/8, d_y_1/8);
            painter.drawLine(0, 0, d_y_1/8, d_y_1/8);
        if self.direction == 'T->G':
            painter.drawLine(0, d_y_1*2/3, 0 ,-d_y_1*2/3);
            painter.drawLine(0, 0, -d_y_1/8, -d_y_1/8);
            painter.drawLine(0, 0, d_y_1/8, -d_y_1/8);

    def drawScan(self,painter):
        h = self.height()/2
        w = self.width()/2
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        # 移动坐标系到中心
        painter.translate(w,h)
        # 系数
        x = (8 / 10) * w
        y = (8 / 10) * h
        painter.setFont(QFont("Arial", 12, QFont.Bold))
        # painter.setBrush(QColor(125, 255, 255));
        painter.drawText(-x/2, -y, x/1, -y/4, Qt.AlignCenter, '扫描示意图')
        # 画矩形 - 水箱
        painter.setBrush(QColor(125, 255, 255));
        painter.drawRect(-x, -y, 2*x, 2*y);
        # painter.drawRect(0, 0, x, y);
        # 画矩形 - 射野
        size_x, size_y = x*2.5/3,y*2.5/3
        painter.setBrush(QColor(25, 5, 155));
        painter.drawRect(-size_x, -size_y, 2*size_x, 2*size_y);
        painter.setFont(QFont("Arial", 10, QFont.Bold))
        # penW = int(10-10*len(self.scanPoints)/1600)
        # if penW<2:
            # penW = 2
        if len(self.scanPoints_x)<50:
            painter.setPen(QPen(QColor(255, 5, 0), 5))
        else:
            painter.setPen(QPen(QColor(255, 5, 0), 2))
        # 移动坐标系到水箱左上角
        # painter.translate(-size_x,-size_y)
        # 画出所有点
        for i in range(0,len(self.scanPoints_x)):
            for j in range(0,len(self.scanPoints_x[i])):
                painter.drawPoint(self.scanPoints_x[i][j]*size_x*2/self.raySize/10,self.scanPoints_y[i][j]*size_y*2/self.raySize/10)
        # 画出运动当前点
        painter.setPen(QPen(QColor(5, 255, 0), 8));
        try:
            for i in range(0,len(self.scanPoints_x)):
                painter.drawPoint(self.scanPoints_x[i][self.scan_point_index]*size_x*2/self.raySize/10,self.scanPoints_y[i][self.scan_point_index]*size_y*2/self.raySize/10)
        except:
            pass

    def TimerMove(self):
        if self.pdd_oar_ray == 'scan':
            self.scan_point_index +=1
            if self.scan_point_index >= len(self.scanPoints_x):
                self.scan_point_index = 0
            self.update()

    def updatePddRoad(self,step,pos,road):
        self.step = step
        self.pos = pos
        self.road = road
        self.update()

    def updateOarRoad(self,step,pos,road):
        self.step = step
        self.pos = pos
        self.road = road
        self.update()

    def updateRaySize(self,size):
        self.raySize = size
        self.update()

    def updateScanRange(self,size,step,direction):
        self.raySize = size
        self.scanStep = step
        self.scanPoints = []
        s = self.raySize*10
        res = int(s) % int(self.scanStep)
        # 分割x,y
        x = np.arange(-s/2,s/2+1,step)
        y = np.arange(-s/2,s/2+1,step)
        if not res == 0:
            x = np.append(x, s/2)
            y = np.append(y, s/2)
        if direction == 'A->B':
            self.scanPoints_x, self.scanPoints_y = np.meshgrid(x, y)    # x-y 平面的网格
        if direction == 'G->T':
            self.scanPoints_y, self.scanPoints_x = np.meshgrid(x, y)    # x-y 平面的网格
        self.update()

    def updateSSD(self,ssd):
        self.SSD = ssd
        self.update()

    def updataAngle(self):
        self.currentValue=self.currentValue+9
        self.m_angle=self.m_angle+6
        # self.m_angle=m_angle+36
        if self.m_angle >360:
            self.m_angle = 0
        # self.currentValue=10
        if self.currentValue >self.maxValue:
            self.currentValue = 0
        self.update()

    def reject(self):
        QDialog.reject(self)

